// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// o [author] rhyme_lph
// Z [email]  rhymelph@gmail.com
// n [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/purchases_summary_entity.dart';
import 'package:finance_app/src/http_service/http_dashboard_service.dart';
import 'package:finance_app/src/mixin/refresh_mixin.dart';
import 'package:finance_app/src/mixin/time_sort_mixin.dart';
import 'package:finance_app/src/mixin/transaction_data_mixin.dart';
import 'package:finance_app/src/page/dashboard/expense/new_expense_page.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:flutter/material.dart';

import '../purchases_provider.dart';

class PurchasesExpenseProvider extends BaseProvider<PurchasesSummaryEntity>
    with
        TimeSortMixin,
        TransactionDataMixin<PurchasesSummaryEntity>,
        RefreshMixin<PurchasesSummaryEntity, List<TransactionEntity>> {
  ScrollController controller = ScrollController();

  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      getParentProvider<PurchasesProvider>().handleScrollUpdate(controller);
    });
  }

  @override
  Future get dataFuture => HttpDashboardService.dashboardPurchaseExpenses();

  void onNavViewAll() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage, params: {
      'type': 6,
    });
  }

  @override
  void unFocus() {}

  @override
  List<String> get types => ['expense'];

  @override
  List<TransactionEntity> get dataList => transactionList;

  void onNavPaidExpense() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage,
        params: {'type': 6, 'initFilter': 'Paid Expense'});
  }

  void onNavOverdueExpense() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage,
        params: {'type': 6, 'initFilter': 'Overdue Expense'});
  }

  void onNavUnpaidExpense() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage,
        params: {'type': 6, 'initFilter': 'Unpaid Expense'});
  }

  @override
  void onTransactionDeleted() {
    onRefresh();
  }

  @override
  Future<void> onRefresh() async {
    await post(dataFuture, onSuccess: (result) {
      data = getEntityFromResult<PurchasesSummaryEntity>(result);
      update();
    }, autoHandler: AutoHandler.Toast);
    return super.onRefresh();
  }
}
